package com.insurance.producttype.service.impl;

import com.insurance.producttype.dao.ProductTypeDao;
import com.insurance.producttype.entity.ProductTypeEntity;
import com.insurance.producttype.service.ProductTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import org.springframework.util.CollectionUtils;

/**
 * 产品类型service
 */
@Service("productTypeService")
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeDao, ProductTypeEntity> implements ProductTypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params, String parentId, String name) {
        QueryWrapper<ProductTypeEntity> productTypeQueryWrapper = new QueryWrapper<ProductTypeEntity>();
        productTypeQueryWrapper.eq("parent_id", parentId);
        if(!StringUtils.isBlank(name)){
            productTypeQueryWrapper.like("name", "%"+name+"%");
        }
        IPage<ProductTypeEntity> page = this.page(new Query<ProductTypeEntity>().getPage(params), productTypeQueryWrapper);
        return new PageUtils(page);
    }

    /**
     * 查询产品类目树结构
     *
     * @return
     */
    @Override
    public List<ProductTypeEntity> findProductTypeTree(){
        List<ProductTypeEntity> productTypeEntityList = this.list();
        if(!CollectionUtils.isEmpty(productTypeEntityList)){
            return createProductTypeTree(productTypeEntityList, "0");
        }
        return new ArrayList<ProductTypeEntity>();
    }

    /**
     * 创建树结构
     *
     * @param productTypeTreeList 产品类型数组
     * @param parentId 根节点
     * @return
     */
    public List<ProductTypeEntity> createProductTypeTree(List<ProductTypeEntity> productTypeTreeList, String parentId){
        List<ProductTypeEntity> newTrees = new ArrayList<ProductTypeEntity>();
        if(!StringUtils.isBlank(parentId) && productTypeTreeList != null && productTypeTreeList.size()>0){
            for (ProductTypeEntity pttv : productTypeTreeList) {
                //如果pId为空，则该节点为父节点
                if (pttv.getParentId().equals(parentId)) {
                    //递归获取父节点下的子节点
                    pttv.setChildren(findProductTypeChildrenNode(parentId, pttv.getId(), productTypeTreeList));
                    newTrees.add(pttv);
                }
            }
        }
        return newTrees;
    }

    /**
     * 递归查询子节点
     *
     * @param rootId 根节点
     * @param pId 父节点
     * @param productTypeTreeList 产品类型集合
     * @return
     */
    private List<ProductTypeEntity> findProductTypeChildrenNode(String rootId, String pId, List<ProductTypeEntity> productTypeTreeList){
        List<ProductTypeEntity> newTrees = new ArrayList<ProductTypeEntity>();
        for (ProductTypeEntity pttv : productTypeTreeList) {
            if (pttv.getParentId().equals(rootId)){
                continue;
            }
            if(pttv.getParentId().equals(pId)){
                //递归获取子节点下的子节点，即设置树控件中的children
                pttv.setChildren(findProductTypeChildrenNode(rootId, pttv.getId(), productTypeTreeList));
                newTrees.add(pttv);
            }
        }
        return newTrees;
    }

}